﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Reflection;
using Microsoft.VisualBasic;
namespace UI.WinForm
{
    /// <summary>
    /// 菜单读取类
    /// </summary>
    public class Menu
    {

        /// <summary>
        /// 菜单执行委托
        /// </summary>
        /// <param name="InvokeName">需要执行的类别</param>
        public delegate void menuEventHandler(string InvokeName);
        /// <summary>
        /// 菜单执行委托
        /// </summary>
        public menuEventHandler menuHandler;

        /// <summary>
        /// 主窗体
        /// </summary>
        private System.Windows.Forms.Form mForm = new System.Windows.Forms.Form();
        /// <summary> 
        /// 动态创建菜单 
        /// </summary>  
        public void CreateMenu(System.Windows.Forms.Form form)
        {

            mForm = form;

            //定义一个主菜单  
            MenuStrip mainMenu = form.MainMenuStrip;
            if (mainMenu == null)
            {
                mainMenu = new MenuStrip();
            }
            mainMenu.MouseMove += new MouseEventHandler(mainMenu_MouseMove);
            //mainMenu.MdiWindowListItem = form.windowTSMI;
            DataSet ds = new DataSet();
            //从XML中读取数据。数据结构后面详细讲一下。  
            ds.ReadXml(@"Config/Menu.xml");
            DataView dv = ds.Tables[0].DefaultView;
            //通过DataView来过滤数据首先得到最顶层的菜单  
            dv.RowFilter = "ParentItemID=0";
            for (int i = 0; i < dv.Count; i++)
            {
                //创建一个菜单项  
                ToolStripMenuItem topMenu = new ToolStripMenuItem();

                //给菜单赋Text值。也就是在界面上看到的值。  
                topMenu.Text = dv[i]["Text"].ToString();
                //如果是有下级菜单则通过CreateSubMenu方法来创建下级菜单  
                if (Convert.ToInt16(dv[i]["IsModule"]) == 1)
                {
                    //以ref的方式将顶层菜单传递参数，因为他可以在赋值后再回传。－－也许还有更好的方法^_^.  
                    CreateSubMenu(ref topMenu, Convert.ToInt32(dv[i]["ItemID"]), ds.Tables[0]);
                }
                ////显示应用程序中已打开的 MDI 子窗体列表的菜单项 

                mainMenu.MdiWindowListItem = topMenu;
                //将递归附加好的菜单加到菜单根项上。  
                mainMenu.Items.Add(topMenu);
            }
            mainMenu.Dock = DockStyle.Top;
            //将窗体的MainMenuStrip梆定为mainMenu.  
            form.MainMenuStrip = mainMenu;
            //这句很重要。如果不写这句菜单将不会出现在主窗体中。  
            form.Controls.Add(mainMenu);
        }

        private void mainMenu_MouseMove(object sender, MouseEventArgs e)
        {
            MenuStrip mainMenu = (MenuStrip)sender;

            ToolStripMenuItem tritem = (ToolStripMenuItem)mainMenu.GetItemAt(e.Location);

            mForm.MainMenuStrip.MdiWindowListItem = tritem;
        }



        /// <summary>  
        /// 创建子菜单  
        /// </summary>  
        /// <param name="topMenu">父菜单项</param>  
        /// <param name="ItemID">父菜单的ID</param>  
        /// <param name="dt">所有菜单数据集</param>  
        private void CreateSubMenu(ref ToolStripMenuItem topMenu, int ItemID, DataTable dt)
        {
            DataView dv = new DataView(dt);
            //过滤出当前父菜单下在所有子菜单数据(仅为下一层的)  
            dv.RowFilter = "ParentItemID=" + ItemID.ToString();

            for (int i = 0; i < dv.Count; i++)
            {
                //创建子菜单项  
                ToolStripMenuItem subMenu = new ToolStripMenuItem();



                subMenu.Text = dv[i]["Text"].ToString();
                //如果还有子菜单则继续递归加载。  
                if (Convert.ToInt16(dv[i]["IsModule"]) == 1)
                {
                    //递归调用  
                    CreateSubMenu(ref subMenu, Convert.ToInt32(dv[i]["ItemID"]), dt);
                }
                else
                {
                    //扩展属性可以加任何想要的值。这里用formName属性来加载窗体。  
                    subMenu.Tag = dv[i]["InvokeName"].ToString();
                    //给没有子菜单的菜单项加事件。  
                    subMenu.Click += new EventHandler(subMenu_Click);
                }
                //if (dv[i]["ImageName"].ToString().Length > 0)
                //{
                //    //设置菜单项前面的图票为16X16的图片文件。  
                //    Image img = Image.FromFile(@"..\..\Image\" + dv[i]["ImageName"].ToString());
                //    subMenu.Image = img;
                //    subMenu.Image.Tag = dv[i]["ImageName"].ToString();
                //}
                //将菜单加到顶层菜单下。  
                topMenu.DropDownItems.Add(subMenu);
            }
        }

        /// <summary>  
        /// 菜单单击事件  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        public void subMenu_Click(object sender, EventArgs e)
        {
            //tag属性在这里有用到。  
            string InvokeName = ((ToolStripMenuItem)sender).Tag.ToString();
            ToolStripMenuItem tsmd = (ToolStripMenuItem)sender;
            if (menuHandler != null)
            {
                menuHandler.Invoke(InvokeName);
            }
            //CreateFormInstance(formName);
        }
    }
}
